import java.awt.BasicStroke;
import java.awt.Font;
import java.awt.geom.Point2D;


import fr.lri.swingstates.canvas.CPolyLine;
import fr.lri.swingstates.canvas.CStateMachine;
import fr.lri.swingstates.canvas.CText;
import fr.lri.swingstates.canvas.Canvas;
import fr.lri.swingstates.gestures.AbstractClassifier;
import fr.lri.swingstates.gestures.Gesture;

import fr.lri.swingstates.sm.State;
import fr.lri.swingstates.sm.Transition;
import fr.lri.swingstates.sm.transitions.Drag;
import fr.lri.swingstates.sm.transitions.Press;
import fr.lri.swingstates.sm.transitions.Release;
import fr.lri.swingstates.sm.transitions.TimeOut;

class Recognizer extends CStateMachine {
	private Gesture gesture;
	private CPolyLine ink;
	private Point2D pInit;
	private AbstractClassifier classifier;
	private CText recognizedClass;

	public Recognizer(AbstractClassifier classifier, Canvas canvas) {
		super();
		this.classifier = classifier;
		recognizedClass = canvas.newText(10, 10, "", new Font("Verdana", Font.PLAIN, 14));
		ink = (CPolyLine) canvas.newPolyLine().setPickable(false).setFilled(false);
		ink.setStroke(new BasicStroke(5 ));
		
		gesture = new Gesture();
	}

	public State start = new State() {
		
		Transition begin = new Press(BUTTON3, ">> draw") {
			public void action() {
				System.out.println("test");
				pInit = getPoint();
				ink.reset(pInit.getX(), pInit.getY()).setDrawable(true).aboveAll();
				gesture.reset();
				gesture.addPoint(pInit.getX(), pInit.getY());
			}
		};
		
	};

	public State draw = new State() {
		Transition drag = new Drag(BUTTON3) {
			public void action() {
				ink.lineTo(getPoint().getX(), getPoint().getY());
				gesture.addPoint(getPoint().getX(), getPoint().getY());
				String gc = classifier.classify(gesture);
				if (gc != null) {
					recognizedClass.setText(gc);
				} else {
					recognizedClass.setText("?");
				}
			}
		};

		Transition end = new Release(BUTTON3, ">> feedback") {
			public void action() {
				armTimer(500, false);
				gesture.addPoint(getPoint().getX(), getPoint().getY());
				String gc = classifier.classify(gesture);
				
				if (gc != null) {
					recognizedClass.setText(gc);
				} else {
					recognizedClass.setText("?");
				}
			}
		};

	};

	public State feedback = new State() {
		Transition timeout = new TimeOut(">> start") {
			public void action() {
				ink.setDrawable(false);
			}
		};
	};
	
	
}
